//
//  CCButton.m
//  uicocos
//
//  Created by Serhiy Otroshchenko on 8/11/11.
//  Copyright 2011 sotrosh. All rights reserved.
//

#import "CCButton.h"

@interface CCButton() 

@property (nonatomic, retain) NSMutableDictionary *sprites;
@property (nonatomic, retain) NSMutableDictionary *titles;
@property (nonatomic, retain) CCLabel *currentTitle;
@property (nonatomic, retain) CCSprite *currentSprite;

@end

@implementation CCButton

@synthesize sprites;
@synthesize titles;
@synthesize currentTitle;
@synthesize currentSprite;
@synthesize disabled;
@synthesize highlighted;

- (id)initWithFrame:(CGRect)aFrame {

    self = [super init];
    
    if (self != nil) {
  
        NSArray *spriteObjects = [NSArray arrayWithObjects:
                                  [CCSprite spriteWithFile:@"Icon.png"],
                                  [CCSprite spriteWithFile:@"Icon-Small.png"],
                                  [CCSprite spriteWithFile:@"Icon-Small-50.png"],
                                  nil];

        NSArray *stateKeys = [NSArray arrayWithObjects:
                              [NSNumber numberWithInt:CCControlStateNormal],
                              [NSNumber numberWithInt:CCControlStateDisabled],
                              [NSNumber numberWithInt:CCControlStateHighlighted],
                              nil];

        NSArray *titleObjects = [NSArray arrayWithObjects:
                                @"Normal Button",
                                @"Disabled Button",
                                 @"Highlighted Button",
                                 nil];
        
        sprites = [[NSMutableDictionary alloc] initWithObjects:spriteObjects forKeys:stateKeys];
        titles = [[NSMutableDictionary alloc] initWithObjects:titleObjects forKeys:stateKeys];
        
        self.disabled = NO;
        self.highlighted = NO;
        
        [self addChild:currentSprite];
        [self addChild:currentTitle];
        
        self.position = aFrame.origin;
        
        self.isTouchEnabled = YES;
    }
    
    return self;
}

- (void)setSprite:(CCSprite *)aSprite forState:(CCControlState)aState {

    NSParameterAssert(aSprite);
    
    
}

- (void)setTitle:(NSString *)aTitle forState:(CCControlState)aState {

    NSParameterAssert(aTitle);
}

- (void)setDisabled:(BOOL)aDisabled {

    self.currentSprite = [sprites objectForKey:[NSNumber numberWithInteger:(NSInteger)aDisabled]];
    self.currentTitle = [titles objectForKey:[NSNumber numberWithInteger:(NSInteger)aDisabled]];

    disabled = aDisabled;
}

- (void)setHighlighted:(BOOL)aHighlighted {
    
    if (aHighlighted) {

        self.currentSprite = [sprites objectForKey:[NSNumber numberWithInteger:CCControlStateHighlighted]];
        self.currentTitle = [titles objectForKey:[NSNumber numberWithInteger:CCControlStateHighlighted]];
    }
    
    highlighted = aHighlighted;
}


#pragma Touch Delegate

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    self.highlighted = YES;
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    self.highlighted = NO;
}



@end
